--等级福利

--#include "data\config\welfare\LevelUpWelfareConfig.lua" once

LevelUpWelfareNormalIdx = 1
LevelUpWelfareExtralIdx = 2

function IsLevelUpWelfareOpen(sysarg)
   return Actor.checkActorLevel(sysarg, LevelUpWelfareCfg.OpenLevel[2], LevelUpWelfareCfg.OpenLevel[1])
end


--发送协议
function SendLevelUpWelfareData(sysarg)
   if not IsLevelUpWelfareOpen(sysarg) then
      return
   end

   local npack = DataPack.allocPacket(sysarg,LogicCmd.trSys.sysId,LogicCmd.trSys.sub.sLevelUpWelfareData)  --申请一个数据包
   if (npack == nil) then
      --print("npack:nil")
      return
   end

   local sVar = Actor.getStaticVar(sysarg)
   if not sVar.LevelUpWelfareState then
      sVar.LevelUpWelfareState = {}   
   end

   local len = #LevelUpWelfareCfg.Awards
   DataPack.writeByte(npack, len)
   for idx = 1, len do
      if not sVar.LevelUpWelfareState[idx] then
        sVar.LevelUpWelfareState[idx] = {}
      end
      local state = sVar.LevelUpWelfareState[idx][LevelUpWelfareNormalIdx] or 0
      local exState = sVar.LevelUpWelfareState[idx][LevelUpWelfareExtralIdx] or 0
      --print("idx = "..idx)
      DataPack.writeByte(npack, idx)
      --print("state = "..state)
      DataPack.writeByte(npack, state)
      --print("exState = "..exState)
      DataPack.writeByte(npack, exState)
   end

   DataPack.flush(npack)   --发送数据包
end

--领取成长好礼
function GetLevelUpWelfareAward(sysarg, packet)
   if not IsLevelUpWelfareOpen(sysarg) then
      return
   end

   local Index = DataPack.readByte(packet)
   --print("Index ="..Index)
   local isEx = DataPack.readByte(packet)
   --print("isEx ="..isEx)
   local ii
    if isEx == 0 then
      ii = LevelUpWelfareNormalIdx
    elseif isEx == 1 then
      ii = LevelUpWelfareExtralIdx
    end

    if not ii then
      Actor.sendTipmsg(sysarg, Lang.ScriptTips.LevelUpWelfare005, ttFlyTip)
      return
    end
   
   if Index > #LevelUpWelfareCfg.Awards then
      Actor.sendTipmsg(sysarg, Lang.ScriptTips.LevelUpWelfare005, ttFlyTip)
      return
   end

   local sVar = Actor.getStaticVar(sysarg)

    if not sVar.LevelUpWelfareState then
        Actor.sendTipmsg(sysarg, Lang.ScriptTips.LevelUpWelfare006, ttFlyTip)
        return
    end
    if not sVar.LevelUpWelfareState[Index] then
        Actor.sendTipmsg(sysarg, Lang.ScriptTips.LevelUpWelfare006, ttFlyTip)
        return
    end
    if not sVar.LevelUpWelfareState[Index][ii] or sVar.LevelUpWelfareState[Index][ii] ~= 1 then
        Actor.sendTipmsg(sysarg, Lang.ScriptTips.LevelUpWelfare006, ttFlyTip)
        return
    end
    local rewards
    if ii == LevelUpWelfareNormalIdx then
      rewards = LevelUpWelfareCfg.Awards[Index].awards
    elseif ii == LevelUpWelfareExtralIdx then

      if not CheckConsumeCond( sysarg, LevelUpWelfareCfg.Awards[Index].exConsume, 1 ) then
            --Actor.sendTipmsg(sysarg, Lang.ScriptTips.coin00001, ttFlyTip)
            return
        end

        if not DoConsumeCond( sysarg, LevelUpWelfareCfg.Awards[Index].exConsume, 1, LogIds.LevelUpWelfareLogId, Lang.LogTips.LevelUpWelfare ) then
            return
        end

      rewards = LevelUpWelfareCfg.Awards[Index].exAwards
    end
     

    sVar.LevelUpWelfareState[Index][ii] = 2
    
    if IsBagGridEnough(sysarg, rewards) then
        GiveCommonAward(sysarg, rewards, LogIds.LevelUpWelfareLogId, Lang.LogTips.LevelUpWelfare)
    else
        local actorId = Actor.getActorId(sysarg)
        local title = Lang.ScriptTips.LevelUpWelfare007
        local content = Lang.ScriptTips.LevelUpWelfare008

        local sex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
        local vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)

        Awards.GiveToMailById(actorId, rewards, title, content, LogIds.LevelUpWelfareLogId, Lang.LogTips.LevelUpWelfare, vocation, sex)
    end

    local npack = DataPack.allocPacket(sysarg,LogicCmd.trSys.sysId,LogicCmd.trSys.sub.sGetLevelUpWelfareAward)  --申请一个数据包
    if (npack == nil) then
        --print("npack:nil")
        return
    end
    --print("Index = "..Index)
    DataPack.writeByte(npack, Index)
    --print("sVar.LevelUpWelfareState[Index][LevelUpWelfareNormalIdx] or 0 = "..sVar.LevelUpWelfareState[Index][LevelUpWelfareNormalIdx])
    DataPack.writeByte(npack, sVar.LevelUpWelfareState[Index][LevelUpWelfareNormalIdx] or 0)
    --print("sVar.LevelUpWelfareState[Index][LevelUpWelfareExtralIdx] or 0 = "..sVar.LevelUpWelfareState[Index][LevelUpWelfareExtralIdx])
    DataPack.writeByte(npack, sVar.LevelUpWelfareState[Index][LevelUpWelfareExtralIdx] or 0)
    DataPack.flush(npack)   --发送数据包
end

--等级改变
function LevelUpWelfareLevelChange(sysarg, circle, level)
  --print("circle = "..circle)
  --print("level = "..level)
   if not IsLevelUpWelfareOpen(sysarg) then
      return
   end
   local sVar = Actor.getStaticVar(sysarg)
   if not sVar.LevelUpWelfareState then
      sVar.LevelUpWelfareState = {}
   end
   local sendData = false
   for k,v in pairs(LevelUpWelfareCfg.Awards) do
      if Actor.checkActorLevel(sysarg, v.level, v.circle) then
        --print("v.circle = "..v.circle)
        --print("v.level = "..v.level)
          if not sVar.LevelUpWelfareState[k] then
              sVar.LevelUpWelfareState[k] = {}
          end
          if sVar.LevelUpWelfareState[k][LevelUpWelfareNormalIdx] and sVar.LevelUpWelfareState[k][LevelUpWelfareNormalIdx] >= 1 then
          else
            sVar.LevelUpWelfareState[k][LevelUpWelfareNormalIdx] = 1
            sendData = true
         end
         if sVar.LevelUpWelfareState[k][LevelUpWelfareExtralIdx] and sVar.LevelUpWelfareState[k][LevelUpWelfareExtralIdx] >= 1 then
          else
            sVar.LevelUpWelfareState[k][LevelUpWelfareExtralIdx] = 1
            sendData = true
         end
      end
   end

   if sendData then
      SendLevelUpWelfareData(sysarg)
   end
end


--等级福利协议处理
LevelUpWelfareMessageHandle = function (sysarg, systemid, msgid, packet)
    if msgid == LogicCmd.trSys.sub.cLevelUpWelfareData then     
        SendLevelUpWelfareData(sysarg)
    elseif msgid == LogicCmd.trSys.sub.cGetLevelUpWelfareAward then
        GetLevelUpWelfareAward(sysarg, packet)
    end
end

